Component({
  properties: {
    goods_id: String,
    bid_list: Object,
    price: Number,
    auction_range: Number,
    is_warn: Number,
    support: Number
  },
  data: {
    discuss_show: false,
    discuss_value: "",
    bail_show: false,
    pay_show: false,
    bid_show: false,
    pay_way: ["微信支付", "余额支付"],
    checked: 0,
  },
  attached() {

  },
  methods: {
    // 讨论内容显示
    clickdiscuss() {
      this.setData({
        discuss_show: true
      })
      this.bidDiscuss()
    },
    // 讨论内容隐藏
    hidediscuss() {
      this.setData({
        discuss_show: false
      })
    },
    // 阻止事件传播
    stopclick() {

    },
    // 讨论数据
    bidDiscuss() {
      var _this = this
      wx.showLoading({
        type: "loading..."
      })
      getApp().request({

        url: getApp().api.auction.bidDiscuss,
        method: "GET",
        data: {
          auction_id: _this.data.goods_id,
        },
        success(res) {
          wx.hideLoading()
          console.log(res)
          _this.setData({
            user_discuss: res.data
          })
        },
        fail(res) {
          console.log(res)
        }
      })
    },
    // 获取讨论输入内容
    inputCont(e) {
      this.setData({
        discuss_value: e.detail.value
      })
    },
    // 发送讨论内容
    clicksend() {
      var _this = this
      getApp().request({
        url: getApp().api.auction.publishComment,
        method: "POST",
        data: {
          auction_id: _this.data.goods_id,
          comment: _this.data.discuss_value
        },
        success(res) {
          _this.setData({
            discuss_value: ""
          })
          _this.bidDiscuss()
        }
      })
    },
    // 点击设置消息提醒
    setremind() {
      if (this.data.is_warn == 0) {
        var is = 1
      } else {
        var is = 0
      }
      var _this = this
      wx.showLoading({
        type: "loading..."
      })
      getApp().request({
        url: getApp().api.auction.warn,
        method: "POST",
        data: {
          auction_id: _this.data.goods_id,
          status: is
        },
        success(res) {
          wx.hideLoading()
          _this.triggerEvent('AgainRequest', _this.data.goods_id);
        }
      })
    },
    // 点击出价
    bidbut() {

      this.bidrequest()
    },
    // 判断是否支付保证金
    bidrequest() {
      var _this = this
      wx.showLoading({
        type: "loading..."
      })
      getApp().request({
        url: getApp().api.auction.supportStatus,
        method: "GET",
        data: {
          auction_id: _this.data.goods_id,
        },
        success(res) {
          wx.hideLoading()
          console.log(res)
          if (res.data.is_support == 1) {
            if (res.data.is_pay == 0) {
              _this.setData({
                bail_show: true
              })
            } else {
              _this.setData({
                bid_show: true,
              })
            }
          } else {
            _this.setData({
              bid_show: true,
            })
          }
        }
      })
      var bid = _this.data.price + _this.data.auction_range
      _this.setData({
        bidprice: bid
      })
    },
    // 保证金隐藏
    clickKonw() {
      this.setData({
        bail_show: false
      })
    },
    // 支付功能
    paybut() {
      this.setData({
        pay_show: true,
        bail_show: false
      })
    },
    payclick(e) {
      var index = e.currentTarget.dataset.index
      this.setData({
        checked: index
      })
    },
    payaffirm() {
      if (this.data.checked == 0) {
        var pay_type = "WECHAT_PAY"
      } else {
        var pay_type = "BALANCE_PAY"
      }
      var _this = this
      wx.showLoading({
        type: "loading..."
      })
      getApp().request({
        url: getApp().api.auction.paySupport,
        method: "POST",
        data: {
          order_id: _this.data.goods_id,
          pay_type: pay_type,
          is_api: 1
        },
        success(res) {
          wx.hideLoading()
          if (_this.data.checked == 0) {
            console.log(res)
            wx.requestPayment({
              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: 'MD5',
              paySign: res.data.paySign,
              success(res) {
                console.log(res)
                _this.setData({
                  pay_way: false,
                  bid_show: true
                })
              },
              fail(res) {
                console.log(res)
              }
            })
          } else {
            _this.setData({
              pay_way: false,
              bid_show: true
            })
          }

        }
      })
    },
    paywayhind() {
      this.setData({
        pay_show: false
      })
    },
    bidhind() {
      this.setData({
        bid_show: false
      })
    },
    reducebid() {
      var bidprice = this.data.price + this.data.auction_range
      if (this.data.bidprice > bidprice) {
        this.setData({
          bidprice: this.data.bidprice - this.data.auction_range
        })
      }
    },
    addbid() {
      this.setData({
        bidprice: this.data.bidprice + this.data.auction_range
      })
    },
    auctionBid() {
      var _this = this
      wx.showLoading({
        type: "loading..."
      })
      getApp().request({
        url: getApp().api.auction.startbid,
        method: "POST",
        data: {
          auction_id: _this.data.goods_id,
          bid: _this.data.bidprice
        },
        success(res) {
          wx.hideLoading()
          _this.triggerEvent('AgainRequest', _this.data.goods_id);
          console.log(res)
        },
        fail(res) {
          console.log(res)
        }
      })
    }
  }
})